package com.zzl.springboot.controller;

import com.zzl.springboot.entity.Admin;
import com.zzl.springboot.entity.User;
import com.zzl.springboot.entity.UserInfo;
import com.zzl.springboot.mapper.UserMapper;
import com.zzl.springboot.service.UserService;
import com.zzl.springboot.utils.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @program: AAAzzl
 * @description:
 * @author: zzl
 * @create: 2022-11-28 16:55
 * @version:1.0
 **/

@Api(tags = "用户模块")
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;



    @RequestMapping("/toHome")
    public String toHome(){
        return "user/home";
    }

    @RequestMapping("/home")
    public String home(){
        return "redirect:/user/toHome";
    }

    @RequestMapping("/index")
    public String index(){
        return "user/index";
    }

    @RequestMapping("/toGetVol")
    public String toGetVol(){
        return "user/getVol";
    }

    @RequestMapping("/toSendVol")
    public String toSendVol(){
        return "user/sendVol";
    }

    @RequestMapping("/toInfo")
    public String toInfo(){
        return "user/info";
    }

    @RequestMapping("/toCommitVol")
    public String toCommitVol(){
        return "user/commitVol";
    }

    @RequestMapping("/toLogin")
    public String toLogin(HttpSession session){
        // fixme : session里面有用户登录数据则不能返回登录
        Object adminInfo = session.getAttribute("userInfo");
        if (adminInfo!=null){
            return "user/home";
        }
        return "user/sign-in";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "user/sign-up";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute("userInfo",null);
        return "redirect:/user/toLogin";
    }


    @ApiOperation("用户登录")
    @RequestMapping("/login")
    @ResponseBody
    public DataResult login(@RequestBody Admin admin, HttpSession session){
        // 根据用户名获取用户信息
        User userInfo = userService.getByUsername(admin.getUsername());
        // 判断密码是否正确
        if (userInfo == null || !admin.getPassword().equals(userInfo.getPassword())){
            // 返回错误信息
            return DataResult.fail("账号或密码错误");
        }
        // 密码正确则获取用户其他数据存入session
        Integer volSend = userService.getVolByUserId(userInfo.getId());
        Integer volGet = userService.getVolUserByUserId(userInfo.getId());
        session.setAttribute("userInfo",userInfo);
        session.setAttribute("volSend",volSend);
        session.setAttribute("volGet",volGet);
        // 返回成功信息
        return DataResult.success(userInfo,null);
    }


    @ApiOperation("用户注册")
    @RequestMapping("/register")
    @ResponseBody
    public DataResult register(@RequestBody User user){
        System.out.println(user.toString());
        User getUser = userService.getByUsername(user.getUsername());
        if (getUser!=null){
            System.out.println("用户已存在");
            return DataResult.fail("用户已存在");
        }
        Integer i = userService.userRegister(user);

        return DataResult.success(i,null);
    }

    @ApiOperation("获取当前用户信息返回给前端")
    @RequestMapping("/getInfo")
    @ResponseBody
    public DataResult getInfo(HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");
        System.out.println(userInfo.toString());
        User user = userService.getUserById(userInfo.getId());
        return DataResult.success(user,null);
    }

    @ApiOperation("用户更新或完善信息")
    @RequestMapping("/updateInfo")
    @ResponseBody
    public DataResult updateInfo(@RequestBody User user){
        System.out.println(user.toString());
        UserInfo userInfo = user.getUserInfo();
        System.out.println(userInfo);
        Integer i = userService.updateInfo(userInfo);
        System.out.println(i);
        if (i==0){
            return DataResult.fail("更新失败");
        }
        User newUser = userService.getUserById(user.getId());
        return DataResult.success(newUser,null);
    }


}
